<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/8/14 0014
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\common\controller\AdminBaseController;
use think\Request;
use think\console\Input;
use think\console\Output;
use app\command\JrkadminCurd;
use think\Exception;
use app\admin\model\Commands;

class Command extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->model = new Commands();
    }


    /**
     * @param Request $request
     * @return string|\think\response\Json
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:添加
     */
    public function add(Request $request){
        if (IS_AJAX) {
            $data = $request->post();
            try{
                $data['command']="php think make:jrkadmin_curd ".$data['controller']." ".$data['model']." ".$data['validate']." --app ".$data['app']."";
                return parent::JsonReturn("生成命令成功",1,"",['data'=>$data['command']]);
            }catch (Exception $exception){
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }
        return $this->fetch();
    }


    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:执行命令
     */
    public function doo(Request $request){
        if (IS_AJAX) {
            $data = $request->post();
            try{
                // dd($data);
                if (empty($data['command'])){
                    return parent::JsonReturn("请先生成命令行", 0);
                }
                $data['admin_id']=self::$admin_info["id"];
                $data['name']="生成菜单";
                $data['do_time']=date("Y-m-d H:i:s",time());
                $data["ext"]=json_encode([$data['controller'],$data['model'],$data['validate'],"--app",$data['app']]);
                $res= $this->model->create($data);
                if ($res){
                    $com=new JrkadminCurd();
                    $input = new Input(json_decode($data["ext"],true));
                    $output=new Output();
                    $com->run($input,$output);
                    return parent::JsonReturn("命令执行成功");
                }else{
                    return parent::JsonReturn("生成命令保存失败", 0);
                }
            }catch (Exception $exception){
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }

    }


}